package kr.hkit.jenny.coffee.service;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.jenny.coffee.dao.CoffeeDao;
import kr.hkit.jenny.coffee.dto.SalesRecord;
import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.myframework.uicomp.ComboPanel;
import kr.hkit.myframework.uicomp.InputPanel;

public class Input extends JFrame implements ActionListener {

	private JPanel contentPane;
	private ComboPanel panel_Code;
	private InputPanel panel_Name;
	private InputPanel panel_Price;
	private InputPanel panel_Amount;
	private InputPanel panel_PMargin;
	private Connection connection;
	private CoffeeDao dao;
	private JButton btnInput;
	private JButton btnPrn01;
	private JButton btnPrn02;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Connection connection = ConnectionFactory
							.getConnection();
					Input frame = new Input(connection);
					frame.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							DbUtil.close(connection);
						}
					});
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Input(Connection con) {
		this.connection = con;
		dao = new CoffeeDao(this.connection);
		initialize();
	}

	private void initialize() {
		setTitle("커피 판매실적");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		panel_Code = new ComboPanel("제품 코드");
		panel.add(panel_Code);
		try {
			panel_Code.addItems(dao.getAllCodes());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		panel_Code.getCb().addItemListener(new ItemListener() {
			// 콤보 박스 값 변경 시 제품명(TF)역시 바뀌는 리스너
			@Override
			public void itemStateChanged(ItemEvent e) {
				try {
					panel_Name.setTf(dao.getName(panel_Code.getSelectItem()));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		panel_Name = new InputPanel("제품 명");
		panel.add(panel_Name);
		panel_Name.getTf().setEnabled(false);
		try {
			panel_Name.setTf(dao.getName(panel_Code.getSelectItem()));
		} catch (SQLException e) {
			e.printStackTrace();
		}

		panel_Price = new InputPanel("제품 단가");
		panel.add(panel_Price);

		panel_Amount = new InputPanel("판매 수량");
		panel.add(panel_Amount);

		panel_PMargin = new InputPanel("마진율");
		panel.add(panel_PMargin);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		btnInput = new JButton("입력");
		btnInput.addActionListener(this);
		panel_1.add(btnInput);

		btnPrn01 = new JButton("출력1");
		btnPrn01.addActionListener(this);
		panel_1.add(btnPrn01);

		btnPrn02 = new JButton("출력2");
		btnPrn02.addActionListener(this);
		panel_1.add(btnPrn02);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnPrn02) {
			actionPerformedBtnPrn02(e);
		}
		if (e.getSource() == btnPrn01) {
			actionPerformedBtnPrn01(e);
		}
		if (e.getSource() == btnInput) {
			actionPerformedBtnInput(e);
		}
	}

	private SalesRecord getRecord() {
		String code = panel_Code.getSelectItem().trim();
		int price = Integer.parseInt(panel_Price.getTf().getText());
		int amount = Integer.parseInt(panel_Amount.getTf().getText());
		double margin = Double.parseDouble(panel_PMargin.getTf().getText());
		return new SalesRecord(code, price, amount, margin);
	}

	protected void actionPerformedBtnInput(ActionEvent e) {
		SalesRecord s = getRecord();
		try {
			dao.insertItem(s);
			JOptionPane.showMessageDialog(this, "입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}
	}

	protected void actionPerformedBtnPrn01(ActionEvent e) {
		ListDlg listDlg = new ListDlg(dao, true);
		listDlg.setVisible(true);
	}
	protected void actionPerformedBtnPrn02(ActionEvent e) {
		ListDlg listDlg = new ListDlg(dao, false);
		listDlg.setVisible(true);
	}
}
